import QtQuick 2.0
import HBaiduPan
import QtQuick.Controls
import Qt.labs.platform 1.1

//下载或上传列表
Window {
    title: "上传下载列表"
    height: 300
    width: 500
    //文件列表
    property var filelist: [/*{size:102,sname:"测试文件"},{size:102,sname:"测试文件2"}*/]
    //当前下载的文件
    property var cfd: null

    BaiduPan {
        id: cpp_pan
        onLoadChange: function(cs){ //下载变化
            //console.log("onLoadChange", cs);
            cfd.csize += cs;
            transview.model = filelist;
        }
        onLoadError: function(e){ //下载出错了
            console.log("onLoadError", e);
            cfd.showstr = e;
            cfd.end = true;
            cfd = null;
            transview.model = filelist;
            loadFile();
        }
        onLoadFinish: { //下载完成
            console.log("onLoadFinish");
            cfd.showstr = "完成";
            cfd.end = true;
            cfd = null;
            if(cfd === filelist[0]){ //下载完成放到最后
                filelist.unshift();
                filelist.push(cfd);
            }
            transview.model = filelist;
            loadFile();
        }
        onUploadChange: function(cs){ //上传变化
            //console.log("onLoadChange", cs);
            cfd.csize += cs;
            transview.model = filelist;
        }
        onUploadError: function(e){ //上传出错了
            console.log("uploadError", e);
            cfd.showstr = e;
            cfd.end = true;
            cfd = null;
            transview.model = filelist;
            loadFile();
        }
        onUploadFinish: { //上传完成
            console.log("uploadFinish");
            cfd.showstr = "完成";
            cfd.end = true;
            cfd = null;
            transview.model = filelist;
            loadFile();
            panreload.clicked();
        }
    }

    MessageDialog {
        id: confirm
        text: "确定要删除正在下载的文件"
        buttons: MessageDialog.Ok | MessageDialog.Cancel
        onOkClicked: {

        }
        onCancelClicked: { //不需要干嘛
        }
    }

    ListView { //传输列表
        id: transview
        width: 500
        height: 300
        spacing: 2
        model: filelist
        delegate: Item {
            width: 500
            height: 20
            ProgressBar {
                anchors.fill: parent
                value: (modelData.csize|0) / modelData.size
                Text {
                    height: 20
                    width: parent.width - 30
                    lineHeight: 20
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    text: "[" + (modelData.showstr||((modelData.csize|0) / modelData.size * 100).toFixed(0) + "%") + "]" + modelData.sname
                    //elide: Text.ElideRight
                }
                ToolButton {
                    anchors.right: parent.right
                    anchors.rightMargin: 5
                    height: 20
                    font.family: "fontello"
                    text: "\uE802"
                    onClicked: {
                        if(filelist[index] === cfd){
                            confirm.open();
                        }else{
                            filelist.splice(index, 1);
                            transview.model = filelist;
                        }
                    }
                }
            }
        }
    }
    //添加下载文件
    function pushfile(fd){
        filelist.push(fd);
        transview.model = filelist;
        loadFile();
    }
    //添加下载文件列表
    function pushfiles(fds){
        //console.log(JSON.stringify(fds));
        for(let fd of fds){
            filelist.push(fd);
            //console.log(JSON.stringify(fd));
        }
        filelist.sort((a,b)=>{return (a.end|0) - (b.end|0)});
        transview.model = filelist;
        loadFile();
    }
    //下载列表中文件
    function loadFile(){
        if(!cfd){
            for(let fd of filelist){
                if(!fd.end){
                    cfd = fd;
                    //console.log(JSON.stringify(fd));
                    if(fd.isdir){
                        cpp_pan.createLocalPath(fd.savepath + "/" + fd.sname);
                    }else if(fd.fileName){ //上传文件
                        fd.csize = 0;
                        cpp_pan.upload(fd.lpath + "/" + fd.fileName, fd.savepath);
                    }else{ //下载文件
                        fd.csize = 0;
                        //console.log(JSON.stringify(fd), fd.fs_id)
                        cpp_pan.download(fd.savepath + "/" + fd.sname, "[" + fd.fs_id + "]");
                    }
                    break;
                }
            }
        }
    }
}
